home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / DbDataSource.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  9.6 KB  |  341 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Event;
  4. import java.awt.Image;
  5.  
  6. public class DbDataSource implements DataSource {
  7.    Matrix rowCache = new Matrix();
  8.    Grid view;
  9.    DbDataStore store;
  10.    DbDataUpdater updater;
  11.    MetaTable meta;
  12.    boolean caching = false;
  13.    Data currData;
  14.    int currDataRow;
  15.    int currDataCol;
  16.  
  17.    public DbDataSource(DbDataStore s, DbDataUpdater u, MetaTable m) {
  18.       this.setupSource(s, u, m);
  19.    }
  20.  
  21.    public Grid getView() {
  22.       return this.view;
  23.    }
  24.  
  25.    public void setupSource(DbDataStore s, DbDataUpdater u, MetaTable m) {
  26.       this.store = s;
  27.       this.store.setDbDataSource(this);
  28.       this.updater = u;
  29.       this.meta = m;
  30.       if (this.meta != null) {
  31.          this.meta.setDbDataSource(this);
  32.       }
  33.  
  34.       this.caching = !this.store.supportsCaching();
  35.    }
  36.  
  37.    public void setGrid(Grid v) {
  38.       this.view = v;
  39.    }
  40.  
  41.    public void setDefaultData(Data defaultValue) {
  42.    }
  43.  
  44.    public void setDefaultData() {
  45.    }
  46.  
  47.    public boolean supportsMeta() {
  48.       return this.meta != null;
  49.    }
  50.  
  51.    public Matrix getCache() {
  52.       return this.rowCache;
  53.    }
  54.  
  55.    public int lastCachedRow() {
  56.       return this.rowCache.rows() - 1;
  57.    }
  58.  
  59.    public MetaTable getMetaTable() {
  60.       return this.meta;
  61.    }
  62.  
  63.    public void setupGrid(Grid v) throws TypeNotSupported {
  64.       if (this.meta == null) {
  65.          throw new TypeNotSupported("MetaTable not set");
  66.       } else {
  67.          this.meta.setupGrid(this.view);
  68.       }
  69.    }
  70.  
  71.    public void commitData() throws TypeNotSupported {
  72.       if (this.currData != null && this.currData.changed()) {
  73.          this.setData(this.currDataRow, this.currDataCol - 1, this.currData);
  74.          this.currData.commit();
  75.          this.currData = null;
  76.          this.currDataRow = -1;
  77.       }
  78.  
  79.    }
  80.  
  81.    public void setCurrentRow(int row) throws TypeNotSupported {
  82.       this.store.setCurrentRow(row);
  83.    }
  84.  
  85.    public Data readData(int row, int col) throws DataNotAvailable {
  86.       ++col;
  87.       if (this.currDataRow == row && this.currDataCol == col) {
  88.          return this.currData;
  89.       } else if (this.caching && this.rowCache.contains(row, col)) {
  90.          return (Data)this.rowCache.elementAt(row, col);
  91.       } else {
  92.          Data d = this.store.getData(row, col);
  93.          if (this.caching) {
  94.             this.rowCache.addElement(row, col, d);
  95.             this.markClean(row);
  96.          }
  97.  
  98.          return d;
  99.       }
  100.    }
  101.  
  102.    public Data getData(Coordinate coords) throws DataNotAvailable {
  103.       return this.getData(coords.row, coords.col);
  104.    }
  105.  
  106.    public Data getData(int row, int col) throws DataNotAvailable {
  107.       ++col;
  108.       if (this.currDataRow == row && this.currDataCol == col) {
  109.          return this.currData;
  110.       } else {
  111.          this.currData = this.readData(row, col - 1);
  112.          this.currDataRow = row;
  113.          this.currDataCol = col;
  114.          return this.currData;
  115.       }
  116.    }
  117.  
  118.    public void addResultSetRow(int row, Data[] data) {
  119.       for(int col = 1; col <= data.length; ++col) {
  120.          this.rowCache.updateElement(row, col, data[col - 1]);
  121.       }
  122.  
  123.       this.markClean(row);
  124.    }
  125.  
  126.    public void setData(int row, int col, Data data) throws TypeNotSupported {
  127.       ++col;
  128.       if (this.caching) {
  129.          this.rowCache.updateElement(row, col, data);
  130.          this.markModified(row);
  131.       } else {
  132.          this.store.update(row, col, data);
  133.       }
  134.    }
  135.  
  136.    public void setData(Coordinate coord, Data data) throws TypeNotSupported {
  137.       this.setData(coord.row, coord.col, data);
  138.    }
  139.  
  140.    public String getText(Coordinate coords) throws DataNotAvailable {
  141.       return this.getData(coords).toString();
  142.    }
  143.  
  144.    public void undeleteRow(int row) throws TypeNotSupported {
  145.       if (this.caching) {
  146.          this.markModified(row);
  147.       }
  148.  
  149.       this.updater.undeleteRow(row);
  150.    }
  151.  
  152.    public void deleteRow(int row) throws TypeNotSupported {
  153.       if (this.caching) {
  154.          this.markDeleted(row);
  155.       }
  156.  
  157.       this.updater.deleteRow(row);
  158.    }
  159.  
  160.    public void insertRow(int row) throws TypeNotSupported {
  161.       this.updater.insertRow(row);
  162.    }
  163.  
  164.    public int appendRow() throws TypeNotSupported {
  165.       return this.updater.appendRow();
  166.    }
  167.  
  168.    public boolean supports(Coordinate coords, int type) {
  169.       return type == 1;
  170.    }
  171.  
  172.    public Image getImage(Coordinate coords) throws DataNotAvailable {
  173.       return this.getData(coords).toImage();
  174.    }
  175.  
  176.    public boolean handleEvent(Event e) {
  177.       switch (e.id) {
  178.          case 54:
  179.             this.rollback();
  180.          case 1005:
  181.          default:
  182.             return false;
  183.       }
  184.    }
  185.  
  186.    public void handleException(int row, int col, Exception ex) {
  187.       this.view.handleException(row, col, ex);
  188.    }
  189.  
  190.    public int rowState(int row) {
  191.       return this.store.rowState(row);
  192.    }
  193.  
  194.    public void clear() {
  195.       this.rowCache.removeAllElements();
  196.       this.store.clear();
  197.    }
  198.  
  199.    public void refresh() {
  200.       this.rowCache.removeAllElements();
  201.       this.store.refresh();
  202.    }
  203.  
  204.    public void undoRow(int row) throws TypeNotSupported {
  205.       this.store.undoRow(row);
  206.    }
  207.  
  208.    public void save() throws TypeNotSupported {
  209.       this.updater.save();
  210.    }
  211.  
  212.    public boolean isDataEditable(int row, int col) {
  213.       if (this.meta != null) {
  214.          try {
  215.             return this.meta.isDataEditable(row, col + 1);
  216.          } catch (DataNotAvailable var3) {
  217.             return false;
  218.          }
  219.       } else {
  220.          return true;
  221.       }
  222.    }
  223.  
  224.    protected void markModified(int row) {
  225.       if (this.rowCache.contains(row, 0)) {
  226.          RowState state = (RowState)this.rowCache.elementAt(row, 0);
  227.          state.markModified();
  228.       } else {
  229.          RowState state = new RowState();
  230.          this.rowCache.addElement(row, 0, state);
  231.          state.markNew();
  232.       }
  233.    }
  234.  
  235.    protected void markNew(int row) {
  236.       RowState state;
  237.       if (this.rowCache.contains(row, 0)) {
  238.          state = (RowState)this.rowCache.elementAt(row, 0);
  239.       } else {
  240.          state = new RowState();
  241.          this.rowCache.addElement(row, 0, state);
  242.       }
  243.  
  244.       state.markNew();
  245.    }
  246.  
  247.    protected void markDeleted(int row) {
  248.       RowState state = (RowState)this.rowCache.elementAt(row, 0);
  249.       state.markDeleted();
  250.    }
  251.  
  252.    protected void markClean(int row) {
  253.       RowState state;
  254.       if (this.rowCache.contains(row, 0)) {
  255.          state = (RowState)this.rowCache.elementAt(row, 0);
  256.       } else {
  257.          state = new RowState();
  258.          this.rowCache.addElement(row, 0, state);
  259.       }
  260.  
  261.       state.markClean();
  262.    }
  263.  
  264.    public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  265.       return this.store.validDataRowRange(top, bottom) - 1;
  266.    }
  267.  
  268.    public int rows() {
  269.       return this.store.rowsRetrieved();
  270.    }
  271.  
  272.    public int type(int row, int col) {
  273.       return 1;
  274.    }
  275.  
  276.    public void rollback(int row, int col) {
  277.       this.rollback();
  278.    }
  279.  
  280.    public void rollbackCurrentData() {
  281.       this.rollback();
  282.    }
  283.  
  284.    public void rollback() {
  285.       this.currData = null;
  286.       this.currDataRow = -1;
  287.    }
  288.  
  289.    public void commit(int row, int col) {
  290.    }
  291.  
  292.    public boolean isMasked(int row, int col) {
  293.       return false;
  294.    }
  295.  
  296.    public String getMask(int row, int col) throws TypeNotSupported {
  297.       throw new TypeNotSupported("stubbed");
  298.    }
  299.  
  300.    public boolean supportsChoice(int row, int col) {
  301.       return false;
  302.    }
  303.  
  304.    public Data[] getChoices(int row, int col) throws TypeNotSupported {
  305.       throw new TypeNotSupported("stubbed");
  306.    }
  307.  
  308.    public void setText(int row, int col, String t) {
  309.    }
  310.  
  311.    public void insertChar(int row, int col, int pos, char c) {
  312.    }
  313.  
  314.    public void setText(int row, int col, char c) {
  315.    }
  316.  
  317.    public void appendChar(int row, int col, char c) {
  318.    }
  319.  
  320.    public void clearText(int row, int col) {
  321.    }
  322.  
  323.    public void deleteChar(int row, int col, int pos) {
  324.    }
  325.  
  326.    public String subString(int row, int col, int spos, int epos) {
  327.       return "stubbed";
  328.    }
  329.  
  330.    public void setImage(int row, int col, Image i) {
  331.    }
  332.  
  333.    public String toString(int row, int col) {
  334.       return "stubbed";
  335.    }
  336.  
  337.    public Image toImage(int row, int col) {
  338.       return null;
  339.    }
  340. }
  341.